<template>
  <el-dialog
    :title="`分配【${title}】角色`"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <h3>{{ val }}</h3>
    <!--
          checkbox
              v-model=值
                  数组：选中某项后会添加该项的值(label的值)，取消就删除该项的值
                  非数组：它就是boolean,选中就是true,取消就是false
              label:选中该项后的值

     -->
    <el-checkbox-group v-model="val">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  data() {
    return {
      isShow: false,
      list: [],
      val: [],
      id: '',
      title: ''
    }
  },
  created() {
    // this.getData()
  },
  methods: {
    async getData() {
      if (this.list.length === 0) {
        const res = await sysRole({
          page: 1,
          pagesize: 9999999
        })
        this.list = res.data.rows
        console.log('角色列表', res)
      }
    },
    // 获取员工详情数据
    async getInfo(id) {
      this.id = id
      const res = await sysUser(id)
      this.val = res.data.roleIds
      console.log('员工详情', res)
    },
    // 提交点击
    async submit() {
      await sysUserAssignRoles({
        id: this.id,
        roleIds: this.val
      })
      this.$message.success('分配角色成功')
      this.isShow = false
    },
    // 关闭时清空勾选
    closeEvent() {
      this.val = []
    }
  }
}
</script>
<style></style>
